﻿$(function() {
    autoResize({ dataGrid: '#Grid', callback: Initlist });
    window_edit = $('#Add').window({
        iconCls: 'icon-add',
        zIndex: 1000,
        width: 300,
        height: 130,
        draggable: true,
        resizable: false,
        shadow: true,
        inline: true,
        modal: true,
        title: '添加字典类型',
        collapsible: false,
        minimizable: false,
        maximizable: false,
        closable: true,
        closed: true,
        onOpen: function() {
            $('#Add').parent().css("left", "25%");
            $('#Add').parent().next().css("left", "25%");
            $('#Add').parent().next().css("top", "25%");
            $('#Add').parent().css("top", "25%");
        }
    });
    window_edit_form = $("#Add").find("form");
    $('#Add').show();
});

function Initlist(size) {
    param.Method = "Paged";
    grid = $('#Grid').datagrid({
        tilte: '字典类型信息',
        iconCls: 'icon-save',
        width: size.width,
        height: size.height,
        pageSize: 10,
        pageList: [10],
        nowrap: true,
        striped: true,
        url: "../Handler/AjaxHandle.ashx",
        queryParams: { "json": $.toJSON(param) },
        sortName: 'Id',
        sortOrder: 'desc',
        idField: 'Id',
        frozenColumns: [[{ title: '编号', field: 'Id', width: 80, sortable: true}]],
        columns: [[
                            { title: '类型名称', field: 'TypeName', width: 80 },
                            { title: '类型值', field: 'TypeText', width: 80 }
                        ]],
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        toolbar: [
                    {
                        text: '添加',
                        iconCls: 'icon-add',
                        handler: Add
                    }, '-',
                    {
                        text: '修改',
                        iconCls: 'icon-edit',
                        handler: Update
                    }, '-',
                    {
                        text: '删除',
                        iconCls: 'icon-remove',
                        handler: function() {

                        }
                    }, '-',
                    {
                        text: '查询',
                        iconCls: 'icon-search',
                        handler: Search
                    }
                  ]
    });

}

var operate;
var row;
var window_edit;
var window_edit_form;
var grid;
var param = { AssemblyName: "OA.Bll", ClassName: "OA.Bll.DictionaryTypePresenter", Method: "", Options: {} };
function Add() {
    window_edit.window({ title: '添加字典类型信息', iconCls: 'icon-add', closed: false });
    operate = "add";
    window_edit_form.form("clear");
}
function Update() {
    row = JSHelper.isDatagridSelected($("#Grid"));
    if (row == null) return;
    $('#Add').window({ title: '修改字典类型信息', iconCls: 'icon-edit', closed: false });
}
function Search() {

    $('#Add').window({ title: '查询字典类型信息', iconCls: 'icon-search', closed: false });
}

function Delete() {
    row = JSHelper.isDatagridSelected($("#Grid"));
    if (row == null) return;
    param.Method = "Delete";
    param.Options["Id"] = row.Id;
    if (row != null) {
        $.ajax({
            type: "post",
            url: "../Handler/AjaxHandle.ashx",
            data: $.toJSON(param),
            dataType: "json",
            success: function(data) {
                if (data.flag) {
                    grid.datagrid('reload');
                    $.messager.alert('提示', data.msg, 'info');
                    window_edit.window('close');
                } else {
                    $.messager.alert('错误', data.msg, 'error');
                }
            }
        });
    }

}
function saveData() {
    if (!window_edit_form.form("validate")) return;
    if (operate == "add")
        param.Method = "Add";
    else {
        param.Method = "Update";
        param["Id"] = row.Id;
    }
    param.Options["TypeName"] = $("#TypeName").val().trim();
    param.Options["TypeValue"] = $("#TypeValue").val().trim();
    $.ajax({
        type: "post",
        url: "../Handler/AjaxHandle.ashx",
        data: $.toJSON(param),
        dataType: "json",
        success: function(data) {
            if (data.flag) {
                grid.datagrid('reload');
                $.messager.alert('提示', data.msg, 'info');
                window_edit.window('close');
            } else {
                $.messager.alert('错误', data.msg, 'error');
            }
        }
    });
}